package net.yebaihe.wc;

import net.yebaihe.wc.DataMan.MSGTYPE;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

class MyHelper extends SQLiteOpenHelper
{
	public static final String DB_NAME = "wc";
	public static final int DB_VERSION = 1;
	private Context ctx;
	public MyHelper(Context context,String name,CursorFactory factory, int version)
	{
		super(context, name, factory, version);
		this.ctx=context;
	}
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		arg0.execSQL("CREATE TABLE IF NOT EXISTS users"
				+" (_id INTEGER PRIMARY KEY, email text,passwd text,username text,image blob)");		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		//db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
		//onCreate(db);
	}
	public static MyHelper get(Context ctx) {
		return new MyHelper(ctx,MyHelper.DB_NAME,null,MyHelper.DB_VERSION);
	}
	
	public UserInfoData getCurrUserInfoData(String email) {
		UserInfoData data=null;
		SQLiteDatabase db = getReadableDatabase();
		if (db!=null){
			Cursor c=db.rawQuery( 
				     "SELECT * FROM users WHERE email='"+
				    		 email+"'", null);
			if (c.moveToFirst()){
				data=new UserInfoData();
				data.email=c.getString(1);
				data.username=c.getString(3);
				byte[] in = c.getBlob(4);
				if ((in!=null) && (in.length>0)){
					data.image = BitmapFactory.decodeByteArray(in, 0, in.length);
				}
			}
			c.close();
			db.close();
		}
		return data;
	}
}	

